﻿/// <reference path="AttachedHandler.js" />
/// <reference path="../Browser.js" />

var _DOMReadySupport = {
	OnLoad: 0,
	ScriptDefer: 1,
	DOMContentLoaded: 2
};





function DOMReadyHandler(obj, handler, binding)
{
	this._boundObjKeyDown = this._obj_KeyDown.bind(this);
	this._boundObjKeyUp = this._obj_KeyUp.bind(this);
	this._boundObjTimeout = this._timeout_Callback.bind(this);
	this._boundObjInterval = this._interval_Callback.bind(this);

	this.AttachedHandler(obj, "keypress", handler, binding);
}
DOMReadyHandler.Inherit(AttachedHandler);

if (____a)
{
	DOMReadyHandler.prototype = new AttachedHandler();
	DOMReadyHandler.prototype.AttachedHandler = new AttachedHandler();
}

DOMReadyHandler._support =
	Browser.get_Name() == "Opera"
	?
	_DOMReadySupport.OnLoad
	:
		Browser.get_Name() == "MSIE"
		?
		_DOMReadySupport.ScriptDefer
		:
		_DOMReadySupport.DOMContentLoaded;


DOMReadyHandler.prototype.get_AddType = function get_AddType()
{
	switch (DOMReadyHandler._support)
	{
		case _DOMReadySupport.DOMContentLoaded:
			return "DOMContentLoaded";
		case _DOMReadySupport.OnLoad:
			return "load";
	}
	return this.AttachedHandler.get_AddType.call(this);
}
DOMReadyHandler.prototype.get_AddObject = function get_AddObject()
{
	switch (DOMReadyHandler._support)
	{
		case _DOMReadySupport.OnLoad:
			return window;
	}
	return this.AttachedHandler.get_AddObject.call(this);
}


DOMReadyHandler.prototype._DoAdd = function _DoAdd()
{
	switch (DOMReadyHandler._support)
	{
		case _DOMReadySupport.ScriptDefer:
			var r = Math.round(Math.random() * 1000);
			var id = "scriptDomReady" + r;
	
			document.write('<script type="text/javascript" id="' + id + '" defer="defer" src="javascript:void(0)"><\/script>');
			var scriptDomReady = document.getElementById(id);

			var fnToCall = this._fnToCall;
			scriptDomReady.onreadystatechange = function onreadystatechange()
			{
				if (this.readyState == "complete")
					fnToCall();
			}
			break;
		default:
			this.AttachedHandler._DoAdd.call(this);
			break;
	}
}